﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForm_Dialog
{
    public partial class DialogBoxObjects : Form
    {
        private UserInformation _ui;

        // Declare a read only property
        public UserInformation UserInformation { get { return _ui; } } 

        public DialogBoxObjects()
        {
            InitializeComponent();

            // Initial the object
            _ui = new UserInformation();

            firstNameText.Validated += new EventHandler(firstNameText_Validated);
            lastNameText.Validated += new EventHandler(lastNameText_Validated);
            emailText.Validated += new EventHandler(emailText_Validated);

            this.DialogResult = DialogResult.Cancel;

            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(DialogBoxObjects_KeyDown);
            this.KeyPress += new KeyPressEventHandler(DialogBoxObjects_KeyPress);
            this.KeyUp += new KeyEventHandler(DialogBoxObjects_KeyUp);

            this.firstNameText.KeyPress += new KeyPressEventHandler(firstNameText_KeyPress);
        }

        void firstNameText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Control.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)50:
                    case (char)53:
                    case (char)56:
                        MessageBox.Show("Control.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }            
        }

        void DialogBoxObjects_KeyUp(object sender, KeyEventArgs e)
        {
            
        }

        void DialogBoxObjects_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Form.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)49:
                    case (char)52:
                    case (char)55:
                        MessageBox.Show("Form.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }

        }

        void DialogBoxObjects_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        void emailText_Validated(object sender, EventArgs e)
        {
            _ui.Email = firstNameText.Text.Trim();
        }

        void lastNameText_Validated(object sender, EventArgs e)
        {
            _ui.LastName = lastNameText.Text.Trim();
        }

        void firstNameText_Validated(object sender, EventArgs e)
        {
            _ui.FirstName = firstNameText.Text.Trim();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (_ui.FirstName.Length == 0)
            {
                MessageBox.Show("First name cannot be zero-length.");
                return;
            }
            if (_ui.LastName.Length == 0)
            {
                MessageBox.Show("Last name cannot be zero-length.");
                return;
            }
            if (_ui.Email.Length == 0)
            {
                MessageBox.Show("Email address cannot be zero-length.");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }
    }

    public class UserInformation
    {
        public UserInformation()
        {
            // Initia the value 
            FirstName = "";
            LastName = "";
            Email = "";
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}
